package com.dayaokeji.rhythmschoolstudent.utils;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.dayaokeji.rhythmschoolstudent.client.mine.account.bean.RemoteHostPicker;
import com.dayaokeji.server_api.domain.RemoteHost;
import com.google.gson.Gson;

import me.jessyan.retrofiturlmanager.RetrofitUrlManager;

/**
 * Created by Gorden on 2018/3/22.
 */

public class RemoteHostUtil {

    private static Gson gson;

    static {
        gson = new Gson();
    }


    public static void saveCurrentRemoteHost(RemoteHostPicker remoteHostPicker) {
        String s = gson.toJson(remoteHostPicker);
        PrefUtils.setCurrentRemoteHost(s);
    }

    @Nullable
    public static RemoteHostPicker getCurrentRemoteHost() {
        String currentRemoteHost = PrefUtils.getCurrentRemoteHost();
        if (!TextUtils.isEmpty(currentRemoteHost)) {
            return gson.fromJson(currentRemoteHost,RemoteHostPicker.class);
        }else{
            return null;
        }
    }

    /**
     * 新建进程设置host
     */
    public static void asyncRemoteHost() {
        RemoteHostPicker currentRemoteHost = RemoteHostUtil.getCurrentRemoteHost();
        if (currentRemoteHost != null) {
            String host = currentRemoteHost.getHost();
            if (!TextUtils.isEmpty(host)) {
                RetrofitUrlManager.getInstance().setGlobalDomain(host);
            }
        }
    }
}
